package cn.zhku.user.service;


import java.util.ArrayList;


import cn.zhku.user.dao.UserDao;
import cn.zhku.user.domain.Admin;
import cn.zhku.user.domain.User;

public class UserService {
	UserDao userDao = new UserDao();

	
	public void regist(User form) throws UserException {
			//根据用户名查找用户，返回用户实体
			User user = userDao.findByUserName(form.getUserName());
			if(user != null) throw new UserException("用户名已被注册");
			userDao.addUser(form);
		
	}
	
	public User login(User form) throws UserException {
		User user = userDao.findByUserName(form.getUserName());
		if(user == null)  throw new UserException("用户名不存在");
		System.out.println(user);
		if(!user.getUserPass().equals(form.getUserPass()))
			throw new UserException("密码错误");
		
			
		return user;
	}

	public ArrayList<User> query() {
		return userDao.findAll();
	}

	public void edit(User user) throws UserException {
		userDao.update(user);	
	}

	public void delete(int userId) throws UserException {
		userDao.delete(userId);	
	}

	public void AdminLogin(Admin form) throws UserException {
		Admin admin = userDao.AdminFindByName(form.getName());
		if(admin == null)  throw new UserException("管理员名不存在");
		if(!admin.getName().equals(form.getName()))
			throw new UserException("密码错误");
		
	}


	

}
